<template>
	<view>
		<u-popup v-model="open" mode="center" border-radius="20">
			<view class="wx df-yc" v-if="type=='wx_group'">
				<view class="img">
					<image @longpress="actionSheet" :src="qrcode" mode="widthFix"></image>
				</view>
				<text style="margin-top: 60rpx;" class="fs26">{{title[type]}}</text>
			</view>
			<view class="code df-yc" v-if="type=='closed_beta'">
				<text class="fs36">{{title[type]}}</text>
				<view style="margin-top: 70rpx;" class="input w100">
					<u-input placeholder="邀请码" v-model="value" />
				</view>
				<view @click="writeCode" class="button fs26 white df-c">获取奖励</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name: '',
		components: {

		},
		data: function() {
			return {
				open: false,
				value: "",
				type: '',
				qrcode: "",
				title: {
					wx_group: '微信扫描上方的二维码加入群聊',
					closed_beta: '输入好友邀请码，获得额外奖励'
				}
			}
		},
		methods: {
			show(type, qrcode = "") {
				this.open = true
				this.type = type
				this.qrcode = qrcode
			},
			hide() {
				this.open = false
			},
			writeCode() {
				this.$api('scan.inviteCode', {
					code: this.value
				}).then(res => {
					console.log(res)
					this.$emit("refresh")
					this.hide()
					if (res.code === 1) {
						this.$tools.toast("领取成功")
					}
				})
			},
			actionSheet() {
				const that = this
				uni.showActionSheet({
					title: '标题',
					itemList: ['保存图片'],
					success: e => {
						if (e.tapIndex == 0) {
							that.saveImage()
						}
					}
				});

			},
			async saveImage() {
				let that = this;
				let checkPermission = await that.$tools.checkAppAlbum()
				uni.downloadFile({
					url: this.qrcode,
					success: (res) => {
						if (res.statusCode == 200) {
							if (checkPermission) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: res => {
										that.$tools.toast('保存成功');
										this.$api('user.updatetackcenter', {
											name: 'wx_group'
										}).then(res => {
											if (res.code == 1) {
												uni.$emit("done")
											}
										})
										that.hide()
									},
									fail: err => {
										that.$tools.toast('保存失败');
									}
								});
							}
						}
					}
				})
			},
			async downLoadFile() {

			}
		},
		created() {},
		mounted() {},
	}
</script>

<style scoped lang="scss">
	.code {
		width: 630rpx;
		height: 464rpx;
		padding: 60rpx;
	}

	.wx {
		width: 466rpx;
		height: 483rpx;

		.img {
			width: 255rpx;
			height: 256rpx;
			background-color: pink;
			margin-top: 70rpx;
		}
	}

	.input {
		border-bottom: 1rpx solid rgba($color: #191919, $alpha: 0.1);
	}

	.button {
		width: 354rpx;
		height: 90rpx;
		background: #64222A;
		border-radius: 45rpx;
		margin-top: 60rpx;
	}
</style>
